﻿@using System.CodeDom
@using System.CodeDom.Compiler
@using System.Collections
@using System.Collections.Specialized
@using System.Reflection
@using System.Text
@using Newtonsoft.Json
@using Newtonsoft.Json.Linq
@using Newtonsoft.Json.Serialization
@using OF.JsonRpc
@using OF.JsonRpc.AspNet
@using OF.JsonRpc.ExtensionInject
@using OF.JsonRpc.Reflection
@using OF.JsonRpc.Util
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>JSON-RPC Help  - @Request.Params["name"]</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet"/>
    <link href="~/Content/styles.css" rel="stylesheet"/>
    <link href="~/Scripts/highlight/styles/atom-one-light.css" rel="stylesheet"/>
    <style type="text/css">h4 {margin-top:20px}</style>
</head>
<body>
<nav class="navbar navbar-default" role="banner">
    <div class="container">
        <div class="navbar-header" class="text-center">
            <h2>
                @Html.ActionLink("JSON-RPC Help", "Index")
            </h2>
        </div>
    </div>
</nav>
<div class="container">

    @{
        string methodName = Request.Params["name"];
        if (!Handler.DefaultHandler.MetaData.Services.ContainsKey(methodName))
        {
            <p style="font-size: 24px; color: red">
                Sorry, Not found this method: <code>@methodName</code>
            </p>
        }
        else
        {
            var smdService = Handler.DefaultHandler.MetaData.Services[methodName];
            var methodXmlComment = AssemlbyCommentReader.GetMethodComment(OF.JsonRpc.Handler.DefaultHandler.Handlers[methodName].Method);


            <h4>
                <code>@Request.Params["name"]</code>
            </h4>
        
            <h3>@AssemlbyCommentReader.GetMethodDescprtionComment(methodXmlComment)</h3>
            
            <h4>Parameters:</h4>
            <table class="table-bordered table-condensed" style="margin-left: 40px;">
                <thead>
                <tr style="background-color: gainsboro">
                    <th>Name</th>
                    <th>Type</th>
                    <th>Description</th>
                </tr>
                </thead>
                <tbody>

                @if (smdService.parameters == null || smdService.parameters.Count() == 0)
                {
                    <tr>
                        <td colspan="3">No parameters.</td>
                    </tr>
                }
                @{
                    foreach (SMDAdditionalParameters param in smdService.parameters)
                    {
                        <tr>
                            <td style="color: cadetblue">@param.Name</td>
                            <td style="color: blue">
                                @if (CSharpEntityGenerator.IsServiceObjectType(param.ObjectType) || param.ObjectType.IsGenericType)
                                {
                                    @Html.ActionLink(AssemlbyCommentReader.GetCSharpRepresentation(param.ObjectType), "Type", new {name = param.ObjectType.AssemblyQualifiedName}, new { target ="_blank"})
                                }
                                else
                                {
                                    @AssemlbyCommentReader.GetCSharpRepresentation(param.ObjectType);
                                }
                            </td>
                            <td style="color: gray">@AssemlbyCommentReader.GetMethodParameterComment(methodXmlComment, param.Name)</td>
                        </tr>
                    }
                }
                </tbody>
            </table>

            <h4>Returns:
            <span style="font-size: 14px">
                <span style="color: blue">@if (CSharpEntityGenerator.IsServiceObjectType(smdService.returns.ObjectType) || smdService.returns.ObjectType.IsGenericType)
                                          {
                                              @Html.ActionLink(AssemlbyCommentReader.GetCSharpRepresentation(smdService.returns.ObjectType), "Type", new {name = smdService.returns.ObjectType.AssemblyQualifiedName}, new { target = "_blank" })
                                          }
                                          else
                                          {
                                              @AssemlbyCommentReader.GetCSharpRepresentation(smdService.returns.ObjectType);
                                          }</span>
                <span style="color: gray">@AssemlbyCommentReader.GetMethodReturnsComment(methodXmlComment)</span>
            </span></h4>


            <h4>Sample:</h4>
            <h5>Request: <button class="btn btn-primary" id="btnTryIt">Try it</button></h5>
            <pre class="small" style="background-color: #fafafa"><code contenteditable="true" id="txtRequest" spellcheck="false" class="json">{
    "jsonrpc": "2.0",
    "method": "@methodName",
    "params": {
                @{
                    StringBuilder paramsJsonBuilder = new StringBuilder();
                    for (var i = 0; i < smdService.parameters.Length; i++)
                    {
                        var param = smdService.parameters[i];
                        string paramComment = AssemlbyCommentReader.GetMethodParameterComment(methodXmlComment, param.Name);
                        if (paramComment != null)
                        {
                            paramComment = paramComment.Replace("*/", "*\\/");
                        }
                        paramsJsonBuilder.Append("\"" + param.Name + "\" : "
                                                 + new SampleJsonSerializer().Serialize(new SampleBuilder().BuildSampleObject(param.ObjectType))
                                                 + " /*" + paramComment + " ["+ AssemlbyCommentReader.GetCSharpRepresentation(param.ObjectType) + "]*/");

                        if (i < smdService.parameters.Length - 1)
                        {
                            paramsJsonBuilder.Append(",\r\n");
                        }
                    }

                    StringReader reader = new StringReader(paramsJsonBuilder.ToString());
                    string line = string.Empty;
                    while (line != null)
                    {
                        if (line != string.Empty)
                        {
                            @Html.Raw("\r\n")
                        }
                        line = reader.ReadLine();
                        @Html.Raw("        " + Html.Encode(line));
                    }
                }
    },
    "id": 1,
    "tags": {
        "userid": "test",
        "companycode": "1000",
        "auth_client_id": "test",
        "auth_token": "test"
    }
}</code></pre>


    <div style="display: inline" id="divResponse">
        <h5 style="display: inline">Response:</h5>
        <label id="lbResponseStatus" style="display: inline;color:InfoText"></label>
    </div>
            <pre class="small" style="background-color: #fafafa"><code id="txtResponse" spellcheck="false" class="json">@{
                JsonResponse jsonResponse = new JsonResponse();
                jsonResponse.Id = 1;
                jsonResponse.Result = new SampleBuilder().BuildSampleObject(smdService.returns.ObjectType);
                @Html.Raw(Html.Encode(new SampleJsonSerializer().Serialize(jsonResponse)))
                @*@Html.Raw(JsonConvert.SerializeObject(jsonResponse))*@
            }</code></pre>
        }
    }


</div>
<script src="~/Scripts/jquery.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/highlight/highlight.pack.js"></script>
<script src="~/Scripts/scripts.js"></script>

<script type="text/javascript">
    
    hljs.initHighlightingOnLoad();

    $(function () {
        $('#txtRequest').text(formatJSON($('#txtRequest').text()));
        $('#txtResponse').text(formatJSON($('#txtResponse').text()));

        $('#btnTryIt').click(function() {
            $('#lbResponseStatus').text('In processing, please wait for a moment...');
            var startTime = new Date();
            $.ajax({
                type: 'POST',
                url: 'json.rpc',
                data: $('#txtRequest').text(),
                dataType: 'text'
            }).always(function (data, textStatus, jqXHR) {
                if (textStatus === "error") {
                    data = "<div style=\"color:red\">" + data.responseText + "</div>";
                }
                var endTime = new Date();
                var formatedData = textStatus === "success" ? formatJSON(data) : data;
                $('#txtResponse').text(formatedData);
                var diffms = GetDateDiff(startTime, endTime, 'ms');
                var diffText = diffms + ' milliseconds';
                if (diffms > 1000) {
                    diffText = diffms / 1000 + ' seconds';
                }
                $('#lbResponseStatus').text('Has returned the result length: ' + data.length + ' chars, with the time of ' + diffText);

                if (formatedData.length <= 5000) //JsonRepsone内容太长时，高亮处理容易造成CPU占满卡死浏览器，故超过5000字符就不高亮。
                    hljs.highlightBlock($('#txtResponse')[0]);

                scroll(null, document.getElementById('divResponse').offsetTop);
            });
        });
    });
</script>
    
        
    
</body>
</html>